﻿package com.passioneffect.widgets {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.GradientType;
	import flash.display.MovieClip;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;	

	public class Reflect extends Sprite {

		private var mc : MovieClip;
		private var mcBMP : BitmapData;
		private var reflectionBMP : Bitmap;
		private var bounds : Object;
		private var bmp:DisplayObject;
		private var gradient:MovieClip;
		
		function Reflect(args : Object) {
		
			this.mouseEnabled=false;
			this.mouseChildren=false;
			
			mc = args.mc;
			var alpha : Number = args.alpha / 100;
			var ratio : Number = args.ratio;
			var reflectionDropoff : Number = args.reflectionDropoff;
			var distance : Number = args.distance;
			
			var ow:Number=mc.width;
			var oh:Number=mc.height;
			var os:Number=this.scaleX;
			
			var filters:Array=mc.filters;
			mc.filters=[];
			
			mc.scaleX=mc.scaleY=1;
			this.scaleX=this.scaleY=1;
			
			var mcHeight = mc.height;
			var mcWidth = mc.width;
			
			bounds = new Object();
			bounds.width = mcWidth;
			bounds.height = mcHeight;
			
			mcBMP = new BitmapData(bounds.width, bounds.height, true, 0xFFFFFF);
			mcBMP.draw(mc);
			
			mc.width=ow;
			mc.height=oh;
			mc.filters=filters;
			
			this.scaleX=this.scaleY=os;
			reflectionBMP = new Bitmap(mcBMP);
			
			reflectionBMP.scaleY = -1;
			
			//reflectionBMP.y =distance;
			
			bmp= this.addChild(reflectionBMP);
			
			gradient =new MovieClip();
			gradient.mouseEnabled=false;
			gradient.y=-bounds.height;
			
			addChild(gradient);
			
			//gradient.visible = false;
			//set the values for the gradient fill
			var fillType : String = GradientType.LINEAR;
			var colors : Array = [0xFFFFFF, 0xFFFFFF];
			var alphas : Array = [alpha, 0];
			var ratios : Array = [0, ratio];
			var spreadMethod : String = SpreadMethod.PAD;
			var matr : Matrix = new Matrix();
		  	
			var matrixHeight : Number;
			if (reflectionDropoff <= 0) {
				matrixHeight = bounds.height;
			} else {
				matrixHeight = bounds.height / reflectionDropoff;
			}
			matr.createGradientBox(bounds.width, matrixHeight, (90 / 180) * Math.PI, 0, 0);
		 	
			gradient.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);  
			gradient.graphics.drawRect(0, 0, bounds.width, bounds.height);
			gradient.cacheAsBitmap = true;
			
			bmp.cacheAsBitmap = true;
			bmp.mask = gradient;
			
		}
		
		private function overHandler(event : MouseEvent) : void {
			event.stopImmediatePropagation();
		}

		public function setBounds(w : Number,h : Number) : void {
			bounds.width = w;
			bounds.height = h;
			gradient.width = bounds.width;
			redrawBMP(mc);
		}

		public function redrawBMP(mc : MovieClip) : void {
			mcBMP.dispose();
			mcBMP = new BitmapData(bounds.width, bounds.height, true, 0xFFFFFF);
			mcBMP.draw(mc);
		}

		public function update() : void {
			var osx:Number=this.scaleX;
			var osy:Number=this.scaleY;
			
			var ow:Number=mc.width;
			var oh:Number=mc.height;
			
			mc.scaleX=mc.scaleY=this.scaleX=this.scaleY=1;
			bounds.width=mc.width;
			bounds.height=mc.height;
			mcBMP = new BitmapData(mc.width,mc.height, true, 0xFFFFFF);
			mcBMP.draw(mc);
			reflectionBMP.bitmapData = mcBMP;
			
			this.scaleX=osx;
			this.scaleY=osy;
		
			
			mc.width=ow;
			mc.height=oh;
		}
		
		public function destroy() : void {
			
			if(parent)
			parent.removeChild(this);
			mc=null;
			removeChild(gradient);
			removeChild(bmp);
			
		}
	}
}